package com.perceptivesoftware.licensing.dao.generated;

import com.perceptivesoftware.licensing.domain.generated.*;
import java.sql.Timestamp;
import java.util.List;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.EntityManager;

/**
 * A data access object (DAO) providing persistence and search support for TwUserval entities. Transaction control of the save(), update() and delete()
 * operations must be handled externally by senders of these methods or must be manually added to each of these methods for data to be persisted to the JPA
 * datastore.
 * 
 * @see com.perceptivesoftware.licensing.dao.generated.TwUserval
 * @author MyEclipse Persistence Tools
 */

@SuppressWarnings("unused")
public abstract class TwUservalDAO implements ITwUservalDAO
{
	//property constants
	public static final String USERVAL_USER_NAME = "uservalUserName";
	public static final String USERVAL_USER_DESC = "uservalUserDesc";
	public static final String USERVAL_DELETED = "uservalDeleted";
	public static final String USERVAL_SUPER = "uservalSuper";
	public static final String USERVAL_EMAIL = "uservalEmail";

	protected final Log logger = LogFactory.getLog(getClass());

	protected EntityManager entityManager;

	@PersistenceContext
	public void setEntityManager(EntityManager entityManager)
	{
		this.entityManager = entityManager;
	}

	/**
	 * Perform an initial save of a previously unsaved TwUserval entity. All subsequent persist actions of this entity should use the #update() method. This
	 * operation must be performed within the a database transaction context for the entity's data to be permanently saved to the persistence store, i.e.,
	 * database. This method uses the {@link javax.persistence.EntityManager#persist(Object) EntityManager#persist} operation.
	 * 
	 * <pre>
	 * EntityManagerHelper.beginTransaction();
	 * TwUservalDAO.save(entity);
	 * EntityManagerHelper.commit();
	 * </pre>
	 * 
	 * @param entity
	 *            TwUserval entity to persist
	 * @throws RuntimeException
	 *             when the operation fails
	 */
	public void save(TwUserval entity)
	{

		logger.info("saving TwUserval instance");
		try
		{
			entityManager.persist(entity);

			logger.info("save successful");
		}
		catch (RuntimeException re)
		{

			logger.error("save failed", re);
			throw re;
		}
	}

	/**
	 * Delete a persistent TwUserval entity. This operation must be performed within the a database transaction context for the entity's data to be permanently
	 * deleted from the persistence store, i.e., database. This method uses the {@link javax.persistence.EntityManager#remove(Object) EntityManager#delete}
	 * operation.
	 * 
	 * <pre>
	 * EntityManagerHelper.beginTransaction();
	 * TwUservalDAO.delete(entity);
	 * EntityManagerHelper.commit();
	 * entity = null;
	 * </pre>
	 * 
	 * @param entity
	 *            TwUserval entity to delete
	 * @throws RuntimeException
	 *             when the operation fails
	 */
	public void delete(TwUserval entity)
	{

		logger.info("deleting TwUserval instance");
		try
		{
			TwUserval localEntity = entityManager.getReference(TwUserval.class, entity.getUservalId());
			entityManager.remove(localEntity);

			logger.info("delete successful");
		}
		catch (RuntimeException re)
		{

			logger.error("delete failed", re);
			throw re;
		}
	}

	/**
	 * Persist a previously saved TwUserval entity and return it or a copy of it to the sender. A copy of the TwUserval entity parameter is returned when the
	 * JPA persistence mechanism has not previously been tracking the updated entity. This operation must be performed within the a database transaction context
	 * for the entity's data to be permanently saved to the persistence store, i.e., database. This method uses the
	 * {@link javax.persistence.EntityManager#merge(Object) EntityManager#merge} operation.
	 * 
	 * <pre>
	 * EntityManagerHelper.beginTransaction();
	 * entity = TwUservalDAO.update(entity);
	 * EntityManagerHelper.commit();
	 * </pre>
	 * 
	 * @param entity
	 *            TwUserval entity to update
	 * @returns TwUserval the persisted TwUserval entity instance, may not be the same
	 * @throws RuntimeException
	 *             if the operation fails
	 */
	public TwUserval update(TwUserval entity)
	{

		logger.info("updating TwUserval instance");
		try
		{
			TwUserval result = entityManager.merge(entity);

			logger.info("update successful");
			return result;
		}
		catch (RuntimeException re)
		{

			logger.error("update failed", re);
			throw re;
		}
	}

	public TwUserval findById(String id)
	{
		logger.debug("finding TwUserval instance with id: " + id);
		try
		{
			TwUserval instance = entityManager.find(TwUserval.class, id);
			return instance;
		}
		catch (RuntimeException re)
		{

			logger.error("find failed", re);
			throw re;
		}
	}

	/**
	 * Find all TwUserval entities with a specific property value.
	 * 
	 * @param propertyName
	 *            the name of the TwUserval property to query
	 * @param value
	 *            the property value to match
	 * @param rowStartIdxAndCount
	 *            Optional int varargs. rowStartIdxAndCount[0] specifies the the row index in the query result-set to begin collecting the results.
	 *            rowStartIdxAndCount[1] specifies the the maximum number of results to return.
	 * @return List<TwUserval> found by query
	 */
	@SuppressWarnings("unchecked")
	public List<TwUserval> findByProperty(String propertyName, final Object value, final int... rowStartIdxAndCount)
	{
		logger.debug("finding TwUserval instance with property: " + propertyName + ", value: " + value);
		try
		{
			final String queryString = "select model from TwUserval model where model." + propertyName + "= :propertyValue";
			Query query = entityManager.createQuery(queryString);
			query.setParameter("propertyValue", value);
			if (rowStartIdxAndCount != null && rowStartIdxAndCount.length > 0)
			{
				int rowStartIdx = Math.max(0, rowStartIdxAndCount[0]);
				if (rowStartIdx > 0)
				{
					query.setFirstResult(rowStartIdx);
				}

				if (rowStartIdxAndCount.length > 1)
				{
					int rowCount = Math.max(0, rowStartIdxAndCount[1]);
					if (rowCount > 0)
					{
						query.setMaxResults(rowCount);
					}
				}
			}
			return query.getResultList();
		}
		catch (RuntimeException re)
		{

			logger.error("find by property name failed", re);
			throw re;
		}
	}

	public List<TwUserval> findByUservalUserName(Object uservalUserName, int... rowStartIdxAndCount)
	{
		return findByProperty(USERVAL_USER_NAME, uservalUserName, rowStartIdxAndCount);
	}

	public List<TwUserval> findByUservalUserDesc(Object uservalUserDesc, int... rowStartIdxAndCount)
	{
		return findByProperty(USERVAL_USER_DESC, uservalUserDesc, rowStartIdxAndCount);
	}

	public List<TwUserval> findByUservalDeleted(Object uservalDeleted, int... rowStartIdxAndCount)
	{
		return findByProperty(USERVAL_DELETED, uservalDeleted, rowStartIdxAndCount);
	}

	public List<TwUserval> findByUservalSuper(Object uservalSuper, int... rowStartIdxAndCount)
	{
		return findByProperty(USERVAL_SUPER, uservalSuper, rowStartIdxAndCount);
	}

	public List<TwUserval> findByUservalEmail(Object uservalEmail, int... rowStartIdxAndCount)
	{
		return findByProperty(USERVAL_EMAIL, uservalEmail, rowStartIdxAndCount);
	}

	/**
	 * Find all TwUserval entities.
	 * 
	 * @param rowStartIdxAndCount
	 *            Optional int varargs. rowStartIdxAndCount[0] specifies the the row index in the query result-set to begin collecting the results.
	 *            rowStartIdxAndCount[1] specifies the the maximum count of results to return.
	 * @return List<TwUserval> all TwUserval entities
	 */
	@SuppressWarnings("unchecked")
	public List<TwUserval> findAll(final int... rowStartIdxAndCount)
	{

		logger.debug("finding all TwUserval instances");
		try
		{
			final String queryString = "select model from TwUserval model";
			Query query = entityManager.createQuery(queryString);
			if (rowStartIdxAndCount != null && rowStartIdxAndCount.length > 0)
			{
				int rowStartIdx = Math.max(0, rowStartIdxAndCount[0]);
				if (rowStartIdx > 0)
				{
					query.setFirstResult(rowStartIdx);
				}

				if (rowStartIdxAndCount.length > 1)
				{
					int rowCount = Math.max(0, rowStartIdxAndCount[1]);
					if (rowCount > 0)
					{
						query.setMaxResults(rowCount);
					}
				}
			}
			return query.getResultList();
		}
		catch (RuntimeException re)
		{

			logger.error("find all failed", re);
			throw re;
		}
	}

}